<template>
  <view class="container">
    <!-- 游乐项目选择 -->
    <view class="project-selector">
      <text class="label">游乐项目</text>
      <picker mode="selector" :value="projectIndex" @change="onProjectChange" :range="projects">
        <view class="picker-view">
          {{ projects[projectIndex] }}
          <uni-icons type="arrowdown" size="16" color="#999"></uni-icons>
        </view>
      </picker>
    </view>

    <!-- 项目信息 -->
    <view class="project-info">
      <text class="section-title">项目信息</text>
      <view class="info-item">
        <text class="info-label">项目名称：</text>
        <text class="info-value">{{ currentProject }}</text>
      </view>
      <view class="info-item">
        <text class="info-label">消费套票类型：</text>
        <text class="info-value">计次票，计时票，期限票</text>
      </view>
    </view>

    <!-- 会员可用套票 -->
    <view class="ticket-section">
      <view class="section-header">
        <text class="section-title">会员可用套票</text>
        <button class="query-btn" @click="fetchTickets">查询</button>
      </view>

      <view class="member-info">
        <text class="info-label">姓名：</text>
        <text class="info-value">{{ memberName }}</text>
      </view>

      <!-- 套票列表 -->
      <view class="ticket-list" v-if="tickets.length > 0">
        <radio-group :v-model="selectedTicketId">
          <view class="ticket-item" v-for="(ticket, index) in tickets" :key="ticket.id">
            <radio :value="ticket.id" :checked="index === 0" color="#ff5722" />
            <text class="ticket-name">{{ ticket.name }}</text>
            <text class="ticket-remaining">余量：{{ ticket.remaining }}{{ ticket.unit }}</text>
          </view>
        </radio-group>
      </view>

      <view class="empty-tip" v-else>
        暂无可用套票
      </view>
    </view>

    <!-- 扣减数量 -->
    <view class="deduction-section">
      <text class="label">扣减数量</text>
      <input 
        type="number" 
        class="deduction-input" 
        placeholder="输入扣减数量" 
        v-model="deductionCount"
        min="1"
      />
    </view>

    <!-- 绑定手环 -->
    <view class="bracelet-section">
      <text class="label">绑定手环</text>
      <view class="radio-group">
        <view class="radio-item" @click="handleBraceletChange(false)">
          <radio :checked="!braceletBinding" />
          <text>不绑定</text>
        </view>
        <view class="radio-item" @click="handleBraceletChange(true)">
          <radio :checked="braceletBinding" />
          <text>绑定</text>
        </view>
      </view>
    </view>

    <!-- 确认扣票按钮 -->
    <button class="confirm-btn" @click="confirmDeduction">确认扣票</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 项目数据
      projects: ['闸机1', '闸机2', '闸机3'],
      projectIndex: 0,
      currentProject: '闸机1',
      
      // 会员信息
      memberName: '张三',
      
      // 套票数据
      tickets: [],
      selectedTicketId: '',
      
      // 扣减信息
      deductionCount: 1,
      braceletBinding: false
    };
  },
  onLoad() {
    // 初始加载套票数据
    this.fetchTickets();
  },
  mounted() {
  	uni.setNavigationBarTitle({
  		title: this.$t('title.ticketDeduction')
  	});
  },
  methods: {
    // 切换游乐项目
    onProjectChange(e) {
      this.projectIndex = e.detail.value;
      this.currentProject = this.projects[this.projectIndex];
      // 切换项目后重新获取套票
      this.fetchTickets();
    },
    
    // 请求套票数据
    fetchTickets() {
      // 模拟接口请求
      uni.showLoading({ title: '加载中...' });
      
      // 实际开发中替换为真实接口
      setTimeout(() => {
        // 模拟接口返回数据
        this.tickets = [
          { id: '1', name: '8次票', remaining: 5, unit: '次' },
          { id: '2', name: '100分钟票', remaining: 50, unit: '分钟' },
          { id: '3', name: '月票', remaining: 15, unit: '天' }
        ];
        
        // 默认选中第一个
        if (this.tickets.length > 0) {
          this.selectedTicketId = this.tickets[0].id;
        }
        
        uni.hideLoading();
      }, 800);
    },
    
    // 处理手环绑定切换
    handleBraceletChange(value) {
      this.braceletBinding = value;
    },
    
    // 确认扣票
    confirmDeduction() {
      // 验证数据
      if (!this.selectedTicketId) {
        return uni.showToast({ title: '请选择套票', icon: 'none' });
      }
      
      if (!this.deductionCount || this.deductionCount < 1) {
        return uni.showToast({ title: '请输入正确的扣减数量', icon: 'none' });
      }
      
      // 找到选中的套票
      const selectedTicket = this.tickets.find(t => t.id === this.selectedTicketId);
      
      // 验证扣减数量是否超过余量
      if (this.deductionCount > selectedTicket.remaining) {
        return uni.showToast({ title: '扣减数量不能超过余量', icon: 'none' });
      }
      
      // 提交扣票请求
      uni.showLoading({ title: '处理中...' });
      
      // 实际开发中替换为真实接口
      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({ title: '扣票成功', icon: 'success' });
        
        // 刷新套票数据
        this.fetchTickets();
        // 重置扣减数量
        this.deductionCount = 1;
      }, 1000);
    }
  }
};
</script>

<style scoped>
.container {
	width: 100%;
  padding: 16px;
  background-color: #f5f5f5;
  min-height: 100%;
  box-sizing: border-box;
}

/* 项目选择器 */
.project-selector {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 12px 16px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.label {
  font-size: 14px;
  color: #333;
}

.picker-view {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 14px;
}

/* 项目信息 */
.project-info {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
  display: block;
}

.info-item {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
}

.info-label {
  color: #666;
  flex-shrink: 0;
  font-size: 14px;
  min-width: 100px;
}

.info-value {
  color: #333;
  margin-left: 8px;
  font-size: 14px;
  flex: 1;
}

/* 套票部分 */
.ticket-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  overflow: hidden;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.query-btn {
  background-color: #ff5722;
  color: #fff;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.5;
}

.member-info {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #eee;
}

.ticket-list {
  margin-top: 12px;
  border-radius: 8px;
  background-color: #ffffff;
  overflow: hidden;
  width: 100%;
  box-sizing: border-box;
}

.ticket-list radio-group {
  width: 100%;
  display: block;
}

.ticket-item {
  width: 100%;
  display: flex;
  background-color: #ffffff;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  box-sizing: border-box;
}

.ticket-item:last-child {
  border-bottom: none;
}

.ticket-name {
  flex: 1;
  margin-left: 8px;
  color: #333333;
  font-size: 14px;
}

.ticket-remaining {
  color: #666666;
  font-size: 14px;
  margin-left: auto;
  white-space: nowrap;
}

.empty-tip {
  text-align: center;
  padding: 20px;
  color: #999;
  font-size: 14px;
}

/* 扣减数量 */
.deduction-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.deduction-input {
  width: 200px;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 12px;
  text-align: right;
  box-sizing: border-box;
}

/* 绑定手环 */
.bracelet-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  overflow: hidden;
}

.radio-group {
  margin-top: 12px;
  display: flex;
  flex-direction: row;
}

.radio-item {
  display: flex;
  align-items: center;
  margin-right: 30px;
  margin-bottom: 10px;
}

.radio-item text {
  margin-left: 8px;
  color: #333;
}

/* 修复radio样式 */
uni-radio {
  transform: scale(0.8);
}

/* 确保radio-group正确渲染 */
uni-radio-group {
  display: inline-block;
}

/* 确认按钮 */
.confirm-btn {
  width: 100%;
  height: 48px;
  background-color: #ff5722;
  color: #fff;
  font-size: 16px;
  border-radius: 8px;
  line-height: 48px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(255, 87, 34, 0.3);
  transition: background-color 0.2s, transform 0.1s;
}

.confirm-btn:active {
  background-color: #f4511e;
  transform: scale(0.98);
}
</style>